//
//  TVView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/18.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "TVView.h"

@interface TVView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation TVView

- (void)setButton:(UIButton *)button WithImageName:(NSString *)name
{
    UIImage *image = GET_IAMGE_WITH_NAME(name);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, 30, 30)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
    [button addSubview:imageView];
    imageView = nil;
}

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (void)addHead
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, self.bounds.size.height / 5)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    leftButton.center = CGPointMake(35, view.bounds.size.height / 2);
    [leftButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnon") forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(pressTVOn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    rightButton.center = CGPointMake(view.bounds.size.width - 35, view.bounds.size.height / 2);
    [rightButton setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/turnoff") forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(pressTVOff) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 - 5, 100, 20)];
    label1.text = @"开启电视机";
    label1.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label1.textColor = [UIColor blackColor];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 + 20, 150, 40)];
    label2.text = @"只有开启电视机才可以控制";
    label2.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    label2.textColor = [UIColor lightGrayColor];
    [label2 setLineBreakMode:NSLineBreakByWordWrapping];
    label2.numberOfLines = 0;
    
    [view addSubview:leftButton];
    [view addSubview:rightButton];
    [view addSubview:label1];
    [view addSubview:label2];
    [self.scrollView addSubview:view];
    
    leftButton = nil;
    rightButton = nil;
    label1 = nil;
    label2 = nil;
    view = nil;
}

- (void)addLeft
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 + 35, 50, 20)];
    label.text = @"操作";
    label.textColor = TEXT_GRAY_COLOR;
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [self.scrollView addSubview:label];
    
    UIButton *button;
    for (int i = 0; i < 12; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake((i % 3) * ((self.bounds.size.width -  8) / 6.0 + 1) ,
                                                            self.bounds.size.height / 5.0 + 70 + (i / 3) *((self.bounds.size.width -  8) / 6.0 + 1),
                                                            (self.bounds.size.width -  8) / 6.0, (self.bounds.size.width -  8) / 6.0)];
        button.tag = i;
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if(i <= 8){
            [button setTitle:[NSString stringWithFormat:@"%d",i + 1] forState:UIControlStateNormal];
        }else if (i == 9){
            [button setTitle:@"-/--" forState:UIControlStateNormal];
        }else if( i == 10){
            [button setTitle:@"0" forState:UIControlStateNormal];
        }else{
            [button setTitle:@"删除" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }
        button.userInteractionEnabled = YES;
        [button addTarget:self action:@selector(pressLeftButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    
    label = nil;
    button = nil;
}

- (void)addRight
{
    UIButton *button;
    for (int i = 0; i < 12; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - (3 - (i % 3)) * ((self.bounds.size.width -  8) / 6.0 + 1) ,
                                                            self.bounds.size.height / 5 + 70 + (i / 3) *((self.bounds.size.width -  8) / 6.0 + 1),
                                                            (self.bounds.size.width -  8) / 6.0, (self.bounds.size.width -  8) / 6.0)];
        button.tag = i;
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if(i == 0){
            [button setTitle:@"节目源" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 1){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_up"];
        }else if (i == 2){
            [button setTitle:@"显示" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 3){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_left"];
        }else if (i == 4){
            [button setTitle:@"确认" forState:UIControlStateNormal];
            [button setTitleColor:TEXT_GREEN_COLOR forState:UIControlStateNormal];
        }else if (i == 5){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_right"];
        }else if (i == 6){
            [button setTitle:@"CH-" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 7){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_down"];
        }else if (i == 8){
            [button setTitle:@"CH+" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 9){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_return"];
        }else if (i == 11){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_menu"];
        }
        button.userInteractionEnabled = YES;
        [button addTarget:self action:@selector(pressRightButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    button = nil;
}

- (void)addDown
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5.0 + self.bounds.size.width * 2 / 3.0 + 95, 50, 20)];
    label.text = @"音量";
    label.textColor = TEXT_GRAY_COLOR;
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    [self.scrollView addSubview:label];
    UIButton *button;
    for (int i = 0; i < 3; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(i * (self.bounds.size.width / 3 + 1),
                                                            self.bounds.size.height / 5 + self.bounds.size.width * 2 / 3.0 + 130,
                                                            (self.bounds.size.width - 2) / 3, 50)];
        [button setBackgroundColor:[UIColor whiteColor]];
        button.tag = i + 20;
        UIImage *image;
        if(i == 0){
            image = GET_IAMGE_WITH_NAME(@"Image/fundationView/TV/volume_down_p");
        }else if(i == 1){
            image = GET_IAMGE_WITH_NAME(@"Image/fundationView/TV/volume_mute_n");
            UIImage *image_p = GET_IAMGE_WITH_NAME(@"Image/fundationView/TV/volume_mute_p");
            [button setImage:image_p forState:UIControlStateSelected];
        }else{
            image = GET_IAMGE_WITH_NAME(@"Image/fundationView/TV/volume_up_p");
        }
        button.userInteractionEnabled = YES;
        [button setImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(pressDownButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    label = nil;
    button = nil;
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height / 5 + self.bounds.size.width * 2 / 3.0 + 180);
}

#pragma mark - 按钮响应函数
- (void)pressTVOn
{
    [self.delegate sendRespondByFuctionName:TV_ON];
}

- (void)pressTVOff
{
    [self.delegate sendRespondByFuctionName:TV_OFF];
}

- (void)pressLeftButton:(UIButton *)button
{
    if(button.tag < 9){
        [self.delegate sendRespondByFuctionName:[NSString stringWithFormat:@"TV_%ld",button.tag + 1]];
        NSLog(@"%ld",button.tag + 1);
    }else if(button.tag == 10){
        [self.delegate sendRespondByFuctionName:@"TV_0"];
    }else if(button.tag == 9){
        [self.delegate sendRespondByFuctionName:TV_SP];
    }else if(button.tag == 11){
        //删除按钮
        [self.delegate sendRespondByFuctionName:TV_DEL];
    }
}

- (void)pressRightButton:(UIButton *)button
{
    if(button.tag == 0){
        //节目源
        [self.delegate sendRespondByFuctionName:TV_AV];
    }else if (button.tag == 1){
        //上
        [self.delegate sendRespondByFuctionName:TV_UP];
    }else if (button.tag == 2){
        //显示
        [self.delegate sendRespondByFuctionName:TV_SHOW];
    }else if (button.tag == 3){
        //左
        [self.delegate sendRespondByFuctionName:TV_LEFT];
    }else if (button.tag == 4){
        [self.delegate sendRespondByFuctionName:TV_OK];
    }else if (button.tag == 5){
        //右
        [self.delegate sendRespondByFuctionName:TV_RIGHT];
    }else if (button.tag == 6){
        //ch-
        [self.delegate sendRespondByFuctionName:TV_CH_DOWN];
    }else if (button.tag == 7){
        //下
        [self.delegate sendRespondByFuctionName:TV_DOWN];
    }else if (button.tag == 8){
        //ch+
        [self.delegate sendRespondByFuctionName:TV_CH_UP];
    }else if (button.tag == 9){
        //返回
        [self.delegate sendRespondByFuctionName:TV_BACK];
    }else if (button.tag == 11){
        //菜单
        [self.delegate sendRespondByFuctionName:TV_MENU];
    }
}

- (void)pressDownButton:(UIButton *)button
{
    UIButton *b = (UIButton *)[self viewWithTag:21];
    if(button.tag == 20){
        //减少音量
        if(b.selected == YES){
            b.selected = NO;
        }
        [self.delegate sendRespondByFuctionName:TV_VOICE_DOWN];
    }else if(button.tag == 21){
        //静音
        b.selected = !b.selected;
        [self.delegate sendRespondByFuctionName:TV_QUIET];
    }else{
        //增加音量
        if(b.selected == YES){
            b.selected = NO;
        }
        [self.delegate sendRespondByFuctionName:TV_VOICE_UP];
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        [self addHead];
        [self addLeft];
        [self addRight];
        [self addDown];
    }
    
    return self;
}

@end
